<?php

declare(strict_types=1);

namespace app\controller\system\permission;

use app\BaseController;
use app\service\system\DictService;
use owns\exception\TransException;

/**
 * 系统配置
 */
class Dict extends BaseController
{
    protected $dictService;

    public function initialize()
    {
        $this->dictService = make(DictService::class);
    }
    /**
     * 字典类型列表
     */
    public function typeList()
    {
        return $this->success($this->dictService->typeList($this->request->get()));
    }
    /**
     * 字典类型编辑
     */
    public function typeEdit()
    {
        $data = $this->request->post();
        return $this->success($this->dictService->typeEdit($data, (int) $data['id']));
    }
    /**
     * 字典类型删除
     */
    public function typeDelete()
    {
        $param = $this->request->post();
        return TransException::exec(DictService::class, 'typeDelete', (int) $param['id']);
    }
    /**
     * 字典数据列表
     */
    public function dataList()
    {
        return $this->success($this->dictService->dataList($this->paging()));
    }
    /**
     * 字典数据编辑
     */
    public function dataEdit()
    {
        $data = $this->request->post();
        return $this->success($this->dictService->dataEdit($data, (int) $data['id']));
    }
    /**
     * 字典数据删除
     */
    public function dataDelete()
    {
        $param = $this->request->post();
        $res = $this->dictService->dataDelete([$param['id']]);
        return $res ? $this->success() : $this->error();
    }
    /**
     * 字典数据批量删除
     */
    public function dataDeletes()
    {
        $param = $this->request->post();
        $res = $this->dictService->dataDelete($param['ids']);
        return $res ? $this->success() : $this->error();
    }
}
